﻿using OA.BLL;

using OA.Model;
using OA.Web.Areas.Member.Controllers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

using Z.EntityFramework.Plus;
using Microsoft.Extensions.Caching.Memory;
using OA.Web.Code;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OA.Web.Attribute;

namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class PositionController : BaseMemberController
    {
        private IMemoryCache _cache;
        public PositionController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [HttpGet]
        [MyActionFilter("205002", "岗位管理", "人力资源管理", true, "icongangweishezhi")]
        public IActionResult Index(string keyword)
        {
            ViewBag.Keyword = keyword;
            return View();
        }

        [MyActionFilter("205002", "岗位管理", "人力资源管理", true, "icongangweishezhi")]
        public IActionResult GetData(string keyWord, int page = 1, int limit = 10)
        {
            Expression<Func<Position, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                where = m => m.Name.Contains(keyWord);
            }

            Expression<Func<Position, long>> order = m => m.Sort;
            List<SimplePosition> listPosition = this.GetAll<Position, SimplePosition>(where, SimplePosition.Map);
            List<SimpleDepartment> listDepartment = this.GetAll<Department, SimpleDepartment>(m => true, SimpleDepartment.Map);
            List<SimplePosition> list = listPosition.OrderByDescending(m => m.ID).Skip((page - 1) * limit).Take(limit).ToList();

            foreach (SimplePosition item in list)
            {
                item.DepartmentFullPath = CDepartment.GetFullDepartmentNameByID(item.DepartmentID, listDepartment);
            }

            var result = new
            {
                code = "0",
                msg = "",
                count = listPosition.Count,
                data = list
            };
            return Json(result);
        }

        [HttpGet]
        [MyActionFilter("205002001", "添加岗位", "岗位管理",false,"")]
        public IActionResult Create(int departmentID = 0)
        {
            if (departmentID == 0)
            {
                departmentID = this.dbContextFactory.DbContext.Department.Where(m => m.ParentDepartmentID == 0).Select(m => m.ID).FirstOrDefault();
            }
            return View(new Position() { DepartmentID = departmentID });
        }

        [HttpPost]
        [MyActionFilter("205002001", "添加岗位", "岗位管理", false, "")]
        public IActionResult Create(int[] Department, int sort,string name)
        {
            BaseResult baseResult = new BaseResult();
            Position info = new Position();
            info.Name = name;
            info.Sort = sort;

            if (Department == null|| Department.Length==0)
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个部门";                 
                return Json(baseResult);
            }
            Department = Department.Where(m => m > 0).ToArray();            
            if (Department.Length == 0)
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个部门";
                return Json(baseResult);
            }
            else
            {
                info.DepartmentID = Department[Department.Length - 1];
                Department myDepartment = this.GetInfo<Department>(info.DepartmentID);
                if (myDepartment == null)
                {
                    baseResult.State = 400;
                    baseResult.Msg = "您选择一个非法的部门";
                    return Json(baseResult);
                }                
            }
            if (string.IsNullOrWhiteSpace(info.Name))
            {
                baseResult.State = 400;
                baseResult.Msg = "岗位名称不能为空";
                return Json(baseResult);
            }
            info.Status = 1;
            this.AddInfo<Position>(info);
            baseResult.Msg = "岗位添加成功";
            return Json(baseResult);
        }



        [HttpGet]
        [MyActionFilter("205002002", "编辑岗位", "岗位管理", false, "")]
        public IActionResult Edit(int id)
        {
            Position infoPosition = this.GetInfo<Position>(id);
            if (infoPosition == null)
            {
                infoPosition = new Position();
                infoPosition.DepartmentID = this.dbContextFactory.DbContext.Department.Where(m => m.ParentDepartmentID == 0).Select(m => m.ID).FirstOrDefault();
                return View("Create", infoPosition);
            }
            else
            {
                return View(infoPosition);
            }
        }

        [HttpPost]
        [MyActionFilter("205002002", "编辑岗位", "岗位管理", false, "")]
        public IActionResult Edit(int id,int[] Department,string level, int sort, string name)
        {
            BaseResult baseResult = new BaseResult();
            Position updatePosition = new Position();
            updatePosition.Name = name;
            updatePosition.Sort = sort;
            updatePosition.Level = level;
            if (Department == null || Department.Length == 0)
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个部门";
                return Json(baseResult);
            }
            Department = Department.Where(m => m > 0).ToArray();
            if (Department.Length == 0)
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个部门";
                return Json(baseResult);
            }
            updatePosition.DepartmentID = Department.LastOrDefault();
            Position info = this.GetInfo<Position>(id);
            if (info == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "没有找到原始岗位";
                return Json(baseResult);
            }
             
            if (string.IsNullOrWhiteSpace(updatePosition.Name))
            {
                baseResult.State = 400;
                baseResult.Msg = "岗位名称不能为空";
                return Json(baseResult);
            }
            info.Status = 1;
            this.dbContextFactory.DbContext.Position.Where(m => m.ID == id).Update(m => new Position() { Name = updatePosition.Name, Sort = updatePosition.Sort, DepartmentID = updatePosition.DepartmentID, Level = updatePosition.Level, IsVirtual = updatePosition.IsVirtual });
            this.dbContextFactory.DbContext.SaveChanges();
            baseResult.Msg = "岗位修改成功";
            return Json(baseResult);

        }


        [MyActionFilter("205002003", "岗位排序", "岗位管理", false, "")]
        public IActionResult SetSort(int id, int sort)
        {             
            try
            {
                Position infoPosition = this.GetInfo<Position>(id);
                if (infoPosition == null)
                {
                    return Json(new { State = 400, Message = "没有找到相关的记录" });
                }
                Position updatePosition = new Position();
                updatePosition.Sort = sort;
                this.Update<Position>(infoPosition, updatePosition, new string[] { "Sort" });
                return Json(new { State = 200, Message = "" });

            }
            catch (Exception ex)
            {
                return Json(new { State = 400, Message = "系统异常,请稍后重试" });
            }
        }

        [MyActionFilter("205002004", "岗位启/禁用", "岗位管理", false, "")]
        public IActionResult Enabled(int id)
        {
            try
            {
                Position infoPosition = this.GetInfo<Position>(id);
                if (infoPosition == null)
                {
                    return Json(new { State = 400, Message = "没有找到相关的记录" });
                }
                Position updatePosition = new Position();
                updatePosition.Status = 1;
                this.Update<Position>(infoPosition, updatePosition, new string[] { "Status" });
                return Json(new { State = 200, Message = "" });

            }
            catch (Exception ex)
            {
                return Json(new { State = 400, Message = "系统异常,请稍后重试" });
            }
        }

        [MyActionFilter("205002005", "修改岗位名称", "岗位管理", false, "")]
        public IActionResult SetName(int id, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return Json(new { State = 400, Message = "岗位名称不能为空" });
            }
            try
            {
                Position infoPosition = this.GetInfo<Position>(id);
                if (infoPosition == null)
                {
                    return Json(new { State = 400, Message = "没有找到相关的记录" });
                }
                Position updatePosition = new Position();
                updatePosition.Name = name;
                this.Update<Position>(infoPosition, updatePosition, new string[] { "Name" });
                return Json(new { State = 200, Message = "" });

            }
            catch (Exception ex)
            {
                return Json(new { State = 400, Message = "系统异常,请稍后重试" });
            }
        }


        [HttpGet]
        [MyActionFilter("205002006", "禁用岗位", "岗位管理", false, "")]
        public IActionResult DisEnabled(int id)
        {
             
            try
            {
                Position infoPosition = this.GetInfo<Position>(id);
                if (infoPosition == null)
                {
                    return Json(new { State = 400, Message = "没有找到相关的记录" });
                }
                Position updatePosition = new Position();
                updatePosition.Status = -1;
                this.Update<Position>(infoPosition, updatePosition, new string[] { "Status" });
                return Json(new { State = 200, Message = "" });

            }
            catch (Exception ex)
            {
                return Json(new { State = 400, Message = "系统异常,请稍后重试" });
            }
        }

  

    }
}